from django.db import models
from django.template.loader import render_to_string
from datetime import datetime
from django.utils import timezone


# Create your models here.
class Activity(models.Model):
    activity_title = models.CharField(max_length=50, blank=False)
    activity_start_time = models.DateTimeField(max_length=50, blank=True, null=True)
    activity_end_time = models.DateTimeField(max_length=50, blank=True, null=True)
    activity_place = models.CharField(max_length=100, blank=True)
    activity_content = models.TextField(max_length=10000, blank=True)

    def __str__(self):
        return self.activity_title

    def activity_thumbnails(self):
        activity_pictures = self.activitypicture_set.all()

        return render_to_string('thumbnail.html', {'images': activity_pictures})

    def activity_enroll_count(self):
        all_enroll_count = self.activityenroll_set.all().count()
        return all_enroll_count


class ActivityPicture(models.Model):
    activity = models.ForeignKey(to=Activity)
    activity_picture = models.ImageField(upload_to='activity_pictures/%Y/%m/%d', blank=True)


# 该表记录每个activity对应的报名者信息
class ActivityEnroll(models.Model):
    activity = models.ForeignKey(Activity)

    enroll_name = models.CharField(max_length=10, blank=False)
    enroll_gender = models.CharField(max_length=4, blank=True)
    enroll_phone = models.CharField(max_length=15, blank=True)
    enroll_wechat = models.CharField(max_length=20, blank=True)
    enroll_school = models.CharField(max_length=20, blank=True)


# 问题
# django定义了一个model，怎么获取这个model的主键，django是否会自动创建一个主键
# 一个model里面的每一条记录都有可能包含多张图片，怎么存这个一对多的关系？
